package com.eastmoney.gamecode.controller;

import com.alibaba.fastjson.JSON;
import com.eastmoney.gamecode.enums.MoveTypeEnums;
import com.eastmoney.gamecode.service.GameLogic;
import com.eastmoney.gamecode.vo.GameRequest;
import com.eastmoney.gamecode.vo.GameResponse;
import com.eastmoney.gamecode.vo.Loc;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("") // 定义接口的基本路径
public class PlayController {
    @Autowired
    GameLogic gameLogic;

    @PostMapping(value = "/play", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public GameResponse playGame(@RequestBody String requestBody) {
//        log.info(requestBody);
        GameRequest gameRequest = JSON.parseObject(requestBody, GameRequest.class);

        if (CollectionUtils.isEmpty(gameRequest.getTankSatesVoList())) {
            return new GameResponse(MoveTypeEnums.STOP.getCode(), true);
        }

        resetMapSize(gameRequest);
        return gameLogic.processGame(gameRequest);
    }

    private static void resetMapSize(GameRequest gameRequest) {
        Loc.MAP_X_LIMIT = gameRequest.getMap().xSize();
        Loc.MAP_Y_LIMIT = gameRequest.getMap().ySize();
    }
}
